package service;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import model.Farmaceutico;
import model.Farmacia;

public class FarmaceuticoService {
	
	protected EntityManager entityManager;
	
	public FarmaceuticoService(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public Farmaceutico criaFarmaceutico(String nome) {
		Farmaceutico farmaceutico = new Farmaceutico();
		farmaceutico.setNome(nome);
		
		insereFarmaceutico(farmaceutico);
		return farmaceutico;
	}
	
	public Farmaceutico alteraFarmacia(Long farmaceuticoId,
			Collection<Farmacia> farmacia) {
		Farmaceutico farm = entityManager.getReference(
				Farmaceutico.class, farmaceuticoId);
		if (farm != null) {
			farm.setFarmacia(farmacia);
		}
		return farm;
	}
	
	public Farmaceutico findFarmaceutico(Long id) {
		Farmaceutico farmaceutico = entityManager.getReference(Farmaceutico.class, id);
		return farmaceutico;
	}
	
	public List<Farmaceutico> listaFarmaceuticos() {
		TypedQuery<Farmaceutico> query = entityManager.createQuery(
				"SELECT e FROM Farmaceutico e", Farmaceutico.class);
		return query.getResultList();
	}
	
	public void deletaFarmaceuticos() {
		TypedQuery<Farmaceutico> query = entityManager.createQuery(
				"DELETE FROM Farmaceutico", Farmaceutico.class);
		query.executeUpdate();
	}
	
	public void insereFarmaceutico(Farmaceutico emp) {
		entityManager.persist(emp);
	}
	
	public void removeFarmaceutico(Long id) {
		Farmaceutico farm = findFarmaceutico(id);
		if (farm != null) {
			entityManager.remove(farm);
		}
	}

}
